package com.lb.statck.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lb.statck.common.result.Result;
import com.lb.statck.mapper.image.imageMapper;
import com.lb.statck.pojo.dto.productDTO;
import com.lb.statck.pojo.vo.ProductVo;
import com.lb.statck.pojo.vo.UserVo;
import com.lb.statck.pojo.vo.image;
import com.lb.statck.service.ProductService;
import com.lb.statck.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 @RestController
@RequestMapping("/api/detail")
public class DetailController {
    @Autowired
    private ProductService productService;
    @Autowired
    private imageMapper imageMapper;

    @GetMapping("/getList")
    public Result getList( @RequestParam(value = "itemId") String id) {
      List<ProductVo> result= productService.getList(id);
        //return result;
        Result result1=new Result();
        result1.setCode(200);
        result1.setData(result);
        return result1;
    }
    @GetMapping("/getProductCode")
   public Result getProductCode(@RequestParam(value = "id") String id) {
        QueryWrapper<ProductVo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        ProductVo one = productService.getOne(wrapper);
      Result result=new Result();
      result.setCode(200);
      result.setData(one);
      return result;
    }
    @PostMapping("/insertProduct")
    public Result insertProduct(@RequestBody productDTO productDTO) {
       // QueryWrapper<ProductVo> wrapper = new QueryWrapper<>();
        //wrapper.eq("id", id);
       // ProductVo one = productService.getOne(wrapper);
        ProductVo productVo=new ProductVo();
        BeanUtils.copyProperties(productDTO, productVo,"id");
        LocalDateTime localDateTime = LocalDateTime.now();

        // 格式化 LocalDateTime 为数字字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = localDateTime.format(formatter);
      //  LocalDateTime localDateTime = LocalDateTime.now();
     //   LocalDate localDate = LocalDate.now();
       // String s=localDateTime.toString();
       // productDTO.getUserid();
        String qw=formattedDateTime+productDTO.getUserid();
        productVo.setProductid(qw);
        List<String> uploadedFiles = productDTO.getUploadedFiles();
        String url = uploadedFiles.get(0);
        productVo.setImageurl(url);
        boolean save = productService.save(productVo);

        for (String uploadedFile : uploadedFiles) {
            image i= new image();
                 i.setProductId(productVo.getId());
            i.setImage(uploadedFile);
            imageMapper.insert(i);
        }


        Result result=new Result();
        result.setCode(200);
      //  result.setData(one);
        return result;
    }

}
